package com.lemon.funplus;

import java.util.ArrayList;

import android.accounts.AccountManager;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.lemon.funplus.object.AccountObject;
import com.lemon.funplus.parse.CMD;
import com.lemon.funplus.parse.JsonParser;
import com.lemon.funplus.ulti.AppUtils;
import com.lemon.funplus.ulti.BaseEngine;
import com.lemon.funplus.ulti.PreferenceUtils;
import com.lemon.funplus.ulti.TagName;

public class AccountLoginActivity extends BaseActivity {
	EditText edtPhone, edtPass;
	TextView textCaution;
	String username = "", password = "";
	AccountManager mAccountManager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.account_login);
		initView();
	}

	@Override
	protected void onResume() {
		super.onResume();
		menuFragment.ACCOUNT.select();
		myActionBar.setTitle(getResources().getString(R.string.login));
	}

	void initView() {
		edtPhone = (EditText) findViewById(R.id.login_edittext_user);
		edtPass = (EditText) findViewById(R.id.login_edittext_password);
		textCaution = (TextView) findViewById(R.id.login_textview_caution);
		edtPass.setOnEditorActionListener(new EditText.OnEditorActionListener() {
			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				if (actionId == EditorInfo.IME_ACTION_DONE) {
					login();
				}
				return false;
			}
		});
		findViewById(R.id.login_button_register).setOnClickListener(clickListener);
		findViewById(R.id.login_button_login).setOnClickListener(clickListener);
		findViewById(R.id.login_button_forgetpass).setOnClickListener(clickListener);
	}

	private void login() {
		if (!edtPhone.getText().toString().equals("") && !edtPass.getText().toString().equals("")) {
			textCaution.setText("");
			username = edtPhone.getText().toString();
			password = edtPass.getText().toString();
			new AsyncTaskParseJson().execute("");
		} else
			textCaution.setText("Tài khoản hoặc mật khẩu không hợp lệ!");
	}

	OnClickListener clickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.login_button_login:
				login();
				break;
			case R.id.login_button_forgetpass:
				startActivity(new Intent(AccountLoginActivity.this, AccountForgetPassActivity.class));
				break;
			case R.id.login_button_register:
				startActivity(new Intent(AccountLoginActivity.this, AccountRegistrationActivity.class));
				// overridePendingTransition(R.anim.slide_in_left, R.anim.main);
				break;
			default:
				break;
			}
		}
	};

	public class AsyncTaskParseJson extends AsyncTask<String, String, String> {
		ProgressDialog progressDialog = new ProgressDialog(AccountLoginActivity.this);

		@Override
		protected void onPreExecute() {
			if (!progressDialog.isShowing())
				progressDialog.show();
		}

		@Override
		protected String doInBackground(String... arg0) {
			String str = JsonParser.login(username, password);
			return str;
		}

		@Override
		protected void onPostExecute(String result) {
//			BaseEngine.showLogformLongString("", result);
			if (progressDialog.isShowing())
				progressDialog.hide();
			if (result != null)
				try {
					ArrayList<String> list = JsonParser.readListdata(result);
					if (list != null) {
//						Log.e("", "status: " + list.get(0));
						if (list.get(0).equals(CMD.TAG_SUCCESS)) {
							AccountObject accountObject = JsonParser.readLogin(result);
							AppUtils.SaveAccount(AccountLoginActivity.this, accountObject);
							AccountLoginActivity.this.runOnUiThread(new Runnable() {
								
								@Override
								public void run() {
									menuFragment.toggleAccountstatus();
								}
							});
							onBackPressed();
						} else if (list.get(0).equals(CMD.TAG_NOTP)) {
							new AlertDialog.Builder(AccountLoginActivity.this).setTitle("Thông báo").setMessage(list.get(1))
									.setPositiveButton("Kích hoạt", new DialogInterface.OnClickListener() {
										public void onClick(DialogInterface dialog, int which) {
											Intent intent = new Intent(AccountLoginActivity.this, AccountActiveActivity.class);
											startActivity(intent);
										}
									}).setNegativeButton("Hủy", new DialogInterface.OnClickListener() {
										public void onClick(DialogInterface dialog, int which) {
										}
									}).setIcon(android.R.drawable.ic_dialog_alert).show();

						} else
							Toast.makeText(AccountLoginActivity.this, list.get(1), Toast.LENGTH_SHORT).show();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
		}
	}
}
